package com.jt.www.biz.order_pc.controller;

import com.github.pagehelper.PageInfo;
import com.google.common.collect.Maps;
import com.jt.www.biz.order_pc.service.SxApplicationFormService;
import com.jt.www.exception.ParamException;
import com.jt.www.model.reps.GenericDataResponse;
import com.jt.www.model.reps.GenericListResponse;
import com.jt.www.model.reps.GenericResponse;
import com.jt.www.model.sxappform.SxApplicationFormInfo;
import com.jt.www.model.sxappform.SxApplicationFormParam;
import com.jt.www.model.sxappform.SxApplicationFormQo;
import com.jt.www.model.sxappform.SxApplicationFormVo;
import com.jt.www.util.excel.ExcelUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import net.sf.jxls.transformer.XLSTransformer;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.util.List;
import java.util.Map;

@Api(value = "山西校责投保单管理", tags = {"SX-APPLICATION-FORM-API"}, description = "山西校责投保单管理")
@RestController
@RequestMapping(value = "/sx/application/v1")
public class SxApplicationFormController {

    @Autowired
    private SxApplicationFormService sxApplicationFormService;

    @ApiOperation(value = "保存投保单", httpMethod = "POST")
    @PostMapping(value = "/_bai/save-app-form", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseEntity<GenericResponse> save(@RequestBody List<SxApplicationFormInfo> param) {
        try {
            Integer i = sxApplicationFormService.save(param);
            if (i != 0) {
                return GenericResponse.ok();
            }
            return GenericResponse.ng("投保单保存失败");
        } catch (ParamException e) {
            return GenericResponse.ng(e.getMessage());
        }
    }


    @ApiOperation(value = "管理端，山西校责投保单列表", httpMethod = "POST")
    @PostMapping(value = "/search-app-form",
            consumes = MediaType.APPLICATION_JSON_UTF8_VALUE,
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseEntity<GenericListResponse<SxApplicationFormVo>> searchSxAppFormList(@RequestBody SxApplicationFormParam sxApplicationFormParam) {
        PageInfo result = sxApplicationFormService.searchSxAppFormList(sxApplicationFormParam);
        return GenericListResponse.listAndCount(result.getList(), result.getTotal());
    }

    @ApiOperation(value = "管理端，导出投保单列表", notes = "管理端，导出投保单列表")
    @GetMapping(value = "/down-app-form", produces = {MediaType.APPLICATION_JSON_UTF8_VALUE})
    public void downSxAppFormList(SxApplicationFormQo sxApplicationFormQo, HttpServletResponse response, HttpServletRequest request) {
        //解析结果
        try {
            List<SxApplicationFormVo> appList = sxApplicationFormService.downSxAppFormList(sxApplicationFormQo);
            Map<String, List<SxApplicationFormVo>> beanParams = Maps.newHashMap();
            beanParams.put("items", appList);
            //模板位置
            String path = "excel/templates/sxappformlist.xls";

            Resource resource = new ClassPathResource(path);

            XLSTransformer transformer = new XLSTransformer();
            InputStream inputStream = resource.getInputStream();
            Workbook wb = transformer.transformXLS(inputStream, beanParams);
            String fileName = "山西校责投保单列表.xls";
            ExcelUtils.compParam(fileName, response, request);
            ServletOutputStream outputStream = response.getOutputStream();
            wb.write(outputStream);
            outputStream.flush();
            inputStream.close();
            outputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @ApiOperation(value = "管理端，更新投保状态", httpMethod = "PUT")
    @PutMapping(value = "/udp-app-status/{id}/{status}", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseEntity<GenericResponse> updateSxAppStatus(@ApiParam(value = "投保单id", required = true) @PathVariable Long id,
                                                             @ApiParam(value = "投保单状态 1-未投保，2-已投保", required = true) @PathVariable String status) {
        Integer i = sxApplicationFormService.updateSxAppStatus(id, status);
        if (i != 0) {
            return GenericDataResponse.ok();
        }
        return GenericDataResponse.ng("更新投保单状态失败");
    }

    @ApiOperation(value = "管理端，更新投保是否有效", httpMethod = "PUT")
    @PutMapping(value = "/udp-app-valid/{id}/{isValid}", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseEntity<GenericResponse> updateSxAppValid(@ApiParam(value = "投保单id", required = true) @PathVariable Long id,
                                                            @ApiParam(value = "投保单是否有效 1-是，0-否", required = true) @PathVariable String isValid) {
        Integer i = sxApplicationFormService.updateSxAppValid(id, isValid);
        if (i != 0) {
            return GenericDataResponse.ok();
        }
        return GenericDataResponse.ng("更新投保单是否有效失败");
    }

    @ApiOperation(value = "业务端，下载投保单PDF", notes = "业务端，下载投保单PDF", httpMethod = "POST")
    @PostMapping(value = "/_bai/downAppPdf", produces = {MediaType.APPLICATION_JSON_UTF8_VALUE})
    public void downAppPdf(@RequestBody List<SxApplicationFormInfo> param, HttpServletRequest request, HttpServletResponse response) {
        try {
            sxApplicationFormService.downAppPdf(param, request, response);
        } catch (Exception e) {
            //log.error("生成投保单pdf失败,请稍后重试")xdc;
            e.printStackTrace();
        }
    }

}
